package com.lt.shiropro.exception;

import com.lt.shiropro.result.Result;
import org.apache.shiro.ShiroException;
import org.apache.shiro.authz.AuthorizationException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import javax.servlet.http.HttpServletRequest;

@RestControllerAdvice
public class MyExceptionHandler {

    // 捕捉其他所有异常
    @ExceptionHandler(Exception.class)
    public Object globalException(HttpServletRequest request, Throwable ex) {
        if(ex instanceof AuthorizationException){
            Result<Object> ret = new Result<>();
            ret.setCode(401);
            ret.setMessage("无权限");
            return ret;
        }else {
            Result<Object> ret = new Result<>();
            ret.setCode(401);
            ret.setMessage(ex.getMessage());
            return ret;
        }

    }

}
